﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;


namespace HelpWindow
{
    public partial class HelpWindow : DigitalRune.Windows.Docking.DockableForm
    {
        public HelpWindow()
        {
            InitializeComponent();
            webBrowser.Navigate(new Uri(Application.StartupPath + "\\Help\\index.html", UriKind.Absolute));
            SetButtonState();
        }

        private void SetButtonState()
        {
            toolStripButtonBack.Enabled = webBrowser.CanGoBack;
            toolStripButtonFore.Enabled = webBrowser.CanGoForward;
        }


      
        public void ReloadHelp(String moduleName) 
        {
            try
            {
                if (moduleName == @"Help")
                {
                    return;
                }
                if (File.Exists(Application.StartupPath + "\\Help\\" + moduleName + ".html"))
                {
                    webBrowser.Navigate(new Uri(Application.StartupPath + "\\Help\\" + moduleName + ".html", UriKind.Absolute));
                }
                else
                {
                    toolStripButtonIndex.PerformClick(); //Wenn seite nicht existiert, index aufrufen
                }
                SetButtonState();
            }
            catch(Exception ex)
            {
                Debug.WriteLine(Properties.Resources.CantOpenHelpString + ": " + ex.Message);
            }
        }

        
        public void UpdateChildren(List<dynamic> formList)
        {
            
            foreach (DigitalRune.Windows.Docking.DockableForm child in formList)
            {
                foreach (Control c in child.Controls)
                {
                    SetEvent(c); //Rekursiv das ClickEvent für alle Controls des Fensters setzen
                }
                child.Click += new EventHandler(child_Enter);
            }
            SetButtonState();
        }

        private void SetEvent(Control c)
        {
            if (c.HasChildren)
            {
                foreach (Control cc in c.Controls)
                {
                    SetEvent(cc);
                }
            }
            c.Click +=new EventHandler(child_Enter);
        }

     

        void child_Enter(object sender, EventArgs e)
        {
            if (!this.Visible)
            {
                return; //fenster nicht sichtbar, also muss hilfe auch nicht angezeigt werden
            }
            object parent = sender;
            while (parent.GetType().BaseType != typeof(DigitalRune.Windows.Docking.DockableForm)) //solange weitermachen, bis das ParentControl das Fenster ist
            {
                parent = (parent as Control).Parent;
                if (parent == null)
                {
                    return; //Hier ist was schiefgelaufen, nichts anzeigen und Funktion verlassen.
                }
            }
            ReloadHelp((parent as DigitalRune.Windows.Docking.DockableForm).Text);
        }


        private void toolStripButtonIndex_Click(object sender, EventArgs e)
        {

            HtmlDocument indexDocument = webBrowser.Document;

            HtmlElement headlineElement = indexDocument.CreateElement("p");
            indexDocument.Body.InnerHtml = "";

            HtmlElement imageElement = indexDocument.CreateElement("img");
            imageElement.SetAttribute("src", Application.StartupPath + "\\Help\\qmark_big.png");
            imageElement.SetAttribute("height", "30");
            imageElement.SetAttribute("width", "30");
            imageElement.SetAttribute("style", "vertical-align: middle");
            


            HtmlElement spanElement = indexDocument.CreateElement("span");
            spanElement.SetAttribute("style", "vertical-align:middle");

            HtmlElement headerElement = indexDocument.CreateElement("font");
            headerElement.SetAttribute("size", "30px");
            headerElement.InnerHtml = "&nbsp&nbsp" + Properties.Resources.IndexHeader;
            spanElement.AppendChild(headerElement);


            headlineElement.AppendChild(imageElement);
            headlineElement.AppendChild(spanElement);
            indexDocument.Body.AppendChild(headlineElement);

            HtmlElement lineElement = indexDocument.CreateElement("hr");
            indexDocument.Body.AppendChild(lineElement);

            DirectoryInfo helpDir = new DirectoryInfo("Help");
            if (helpDir.Exists) 
            {
                HtmlElement listElement = indexDocument.CreateElement("ul");
                foreach (var file in helpDir.GetFiles("*.html"))
                {
                    if (file.Name == "index.html")
                    {
                        continue; //Dummypage zum erstellen des Bodys, soll nicht angezeigt werden
                    }
                    HtmlElement linkElement = indexDocument.CreateElement("a");
                    linkElement.SetAttribute("href", file.FullName);
                    linkElement.InnerText = file.Name.Replace(file.Extension, "");

                    HtmlElement listEntryElement = indexDocument.CreateElement("li");
                    listEntryElement.AppendChild(linkElement);
                    listElement.AppendChild(listEntryElement);
                }
                indexDocument.Body.AppendChild(listElement);
                
            }
            SetButtonState();
        }

        private void toolStripButtonBack_Click(object sender, EventArgs e)
        {
            webBrowser.GoBack();
            
            SetButtonState();
        }

        private void toolStripButtonFore_Click(object sender, EventArgs e)
        {
            webBrowser.GoForward();
            FileInfo fi = new FileInfo(webBrowser.Url.AbsolutePath);
            
            SetButtonState();
        }

        private void toolStripButtonPrint_Click(object sender, EventArgs e)
        {
            webBrowser.ShowPrintPreviewDialog();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
    }
}
